import { ref, computed, watchEffect } from 'vue'
import { BASE_COLUMNS, ELECTRIC_COLUMNS, DEFAULT_VISIBLE_COLUMNS, DEFAULT_ELECTRIC_COLUMNS } from './columnConfig'
import { useMeterStore } from '@/stores/meter';
const meterStore = useMeterStore();
const isElectricMeter = computed(() => meterStore.currentMeter?.type === 2 ?? false);

export function useColumnSelect() {
    const selectedColumns = ref([...DEFAULT_VISIBLE_COLUMNS])
    const allColumns = ref([])

    // 按组分类的列选项
    const columnGroups = computed(() => {
        const groups = {}
        allColumns.value.forEach(col => {
            if (!groups[col.group]) {
                groups[col.group] = []
            }
            groups[col.group].push({
                label: col.label,
                value: col.prop
            })
        })
        return Object.entries(groups).map(([group, options]) => ({
            label: group,
            options
        }))
    })

    // 获取当前可见列配置
    const visibleColumns = computed(() => {
        return allColumns.value.filter(col =>
            selectedColumns.value.includes(col.prop) || col.required
        )
    })

    //重置
    const columnReset = () => {
        if (isElectricMeter.value) {
            selectedColumns.value = [...DEFAULT_VISIBLE_COLUMNS, ...DEFAULT_ELECTRIC_COLUMNS]
            allColumns.value = [...BASE_COLUMNS, ...ELECTRIC_COLUMNS]
        } else {
            selectedColumns.value = [...DEFAULT_VISIBLE_COLUMNS]
            allColumns.value = [...BASE_COLUMNS]
        }
    }
    watchEffect(columnReset)

    return {
        selectedColumns,
        columnGroups,
        visibleColumns,
        columnReset
    }
}